﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pahan.McuFusion.IO.Windows
{
    internal class WinRing0ParallelPortImpl : ParallelPortImpl
    {
        #region Overrides of ParallelPortImpl

        /// <summary>
        /// Opens the port.
        /// </summary>
        protected override void DoOpen()
        {
            ols = new OpenLibSys();
            switch (ols.GetStatus())
            {
                case OpenLibSys.Status.NO_ERROR:
                    break;
                case OpenLibSys.Status.DLL_NOT_FOUND:
                    throw new DllNotFoundException("The parallel port interaction library is missing.");
                case OpenLibSys.Status.DLL_INCORRECT_VERSION:
                    throw new InvalidOperationException("The parallel port interaction library has invalid version.");
                case OpenLibSys.Status.DLL_INITIALIZE_ERROR:
                    throw new InvalidOperationException(
                        "The parallel port interaction library was not able to iniatialize.");
            }
        }

        /// <summary>
        /// Closes the port.
        /// </summary>
        protected override void DoClose()
        {
            ols.Dispose();
            ols = null;
        }

        /// <summary>
        /// Reads the value from ther specified i/o port.
        /// </summary>
        protected override byte In(ushort address)
        {
            return ols.ReadIoPortByte(address);
        }

        /// <summary>
        /// Writes the value to the specified i/o port.
        /// </summary>
        protected override void Out(ushort address, byte value)
        {
            ols.WriteIoPortByte(address, value);
        }

        #endregion

        #region Private members

        private OpenLibSys ols;

        #endregion
    }
}
